#include "ViewInputDialog.h"

ViewInputDialog::ViewInputDialog(QWidget *parent) :
    QDialog(parent), pixmapRect(QRect( 0, 0, pixmapWidth, pixmapHeight )),
    indentRect(QRect(indentSize, indentSize, pixmapWidth - 2*indentSize, pixmapHeight - 2*indentSize))
{

    imagePixmap = new QPixmap( pixmapRect.width(), pixmapRect.height() );

    imageLabel = new QLabel;
    imageLabel->setPixmap( *imagePixmap );

    machineNumberLabel = new QLabel(tr("Machine: "));

    machNumberSpin = new QSpinBox;
    machNumberSpin->setFixedWidth( 100 );

    cancelButton = new QPushButton(tr("Close"));

    buttonsLayout = new QHBoxLayout;
    buttonsLayout->addWidget( machineNumberLabel );
    buttonsLayout->addWidget( machNumberSpin );
    buttonsLayout->addStretch(0);
    buttonsLayout->addWidget( cancelButton );

    mainLayout = new QVBoxLayout;
    mainLayout->addWidget( imageLabel );
    mainLayout->addLayout( buttonsLayout );

    setLayout( mainLayout );

    setWindowTitle( tr("Input view") );

    //setWindowState( Qt::WindowMaximized );
    //setFixedSize( size() );

    connect( machNumberSpin, SIGNAL(valueChanged(int)), this, SLOT(drawMachineInput(int)) );
    connect( cancelButton, SIGNAL(clicked()), this, SLOT(close()) );

    //machNumberSpin->setValue( 0 );
}

void ViewInputDialog::setInputTable(const QVector<QVector<int> > &table)
{
    inputTable = table;

    machNumberSpin->setMinimum( 0 );
    machNumberSpin->setMaximum( inputTable.size()-1 );

    //setValue() will emit valueChanged() if the new value is different from the old one.
    machNumberSpin->setValue( 1 );
    machNumberSpin->setValue( 0 );
}

void ViewInputDialog::drawMachineInput(int num)
{
    //if (num < 0) return;

    QPainter painter;
    painter.begin( imagePixmap );

    //calculating column witdh
    int columnWidth = indentRect.width() / inputTable[num].size();

    //calc height for each column
    QVector<int> columnHeight;
    //find max value
    int mx = -1;
    for (int i=0; i<inputTable[0].size(); i++)
        if ( inputTable[num][i] > mx ) mx = inputTable[num][i];
    //calc coef
    double coef = (double)(indentRect.height() - indentSize) / (double)mx;

    int t;
    for (int i=0; i<inputTable[0].size(); i++){
        t = (int)(coef * (double)inputTable[num][i]);
        columnHeight.push_back( t );
    }


    painter.fillRect( imagePixmap->rect(), QBrush(Qt::white) );

    painter.setPen( QPen( QBrush(Qt::black), 2 ) );
    painter.drawLine( indentRect.left(), indentRect.bottom() - indentSize,
                      indentRect.right(), indentRect.bottom() - indentSize );

    painter.setBrush( QBrush( QColor().fromRgb(73, 204, 212) ) );

    QRect colRect;
    QTextOption textOption;
    textOption.setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

    painter.setFont( QFont("Arial", 20) );

    for (int i=0; i<inputTable[0].size(); i++){
        colRect.setRect( indentRect.left() + i*columnWidth,
                         indentRect.bottom() - indentSize - columnHeight[i],
                         columnWidth,
                         columnHeight[i] );

        painter.drawRect( colRect );
        painter.drawText( colRect, QString::number( inputTable[num][i] ), textOption);

        //drawing column downheaders
        painter.drawText( QRect( colRect.left(), colRect.bottom(), columnWidth, 2*indentSize ),
                          QString::number( i ), textOption );
    }

    painter.end();

    imageLabel->setPixmap( *imagePixmap );
}
